﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameProtocol;
using UnityMultiPlayerGameServer.Clients;
using UnityMultiPlayerGameServer.Data;

namespace UnityMultiPlayerGameServer.Manager
{
    internal class RoomController : BaseController
    {
        public RoomController()
        {
            messageType = MessageType.Room;
        }
        public GamePack ActionCreateRoom(Client client, GamePack pack)
        {
            //当前客户端用户没有登录或者要创建的房间已经存在了，返回失败
            if (!client.IsLogin || RoomManager.Instance.isRoomExist(pack.RoomInfo.RoomName))
            {
                pack.ReturnCode = ReturnCode.ReturnFail;
                return pack;    
            }

            //创建房间并添加到房间管理类中
            Room room = new Room(pack.RoomInfo.RoomName, client, pack.RoomInfo.RoomCapacity);
            RoomManager.Instance.AddRoom(room);

            //设置要返回的包的内容
            pack.RoomInfo = room.RoomInfo;
            pack.UserInfo = client.userInfo;

            pack.ReturnCode = ReturnCode.ReturnSucceed;
                                 
            Console.WriteLine("create room :" + room.RoomInfo.RoomName + "  succeed");
            //Console.WriteLine(room.ToString());
            return pack;
        }

        /// <summary>
        /// 将所有房间信息返回给客户端
        /// </summary>
        /// <param name="client"></param>
        /// <param name="pack"></param>
        /// <returns></returns>
        public GamePack ActionSearchRoom(Client client, GamePack pack)
        {  
            pack.ReturnCode = ReturnCode.ReturnSucceed;
            pack.ActionCode = ActionCode.ActionSearchRoom;
            
            pack.RoomList.Add(ActionSearchRoomGetIList());
            Console.WriteLine("ActionSearchRoom:  " + client.Socket.RemoteEndPoint);
            return pack;
        }

        /// <summary>
        /// 将房间管理器中的房间字典转换成IList返回给pack.roomDict赋值
        /// </summary>
        /// <param name="roomDict"></param>
        /// <returns></returns>
        private IList<RoomInfo> ActionSearchRoomGetIList()
        {
            List<RoomInfo> info_list = new List<RoomInfo>();

            Dictionary<string, Room> dict = RoomManager.Instance.GetRoomDict();

            foreach (KeyValuePair<string, Room> room in dict)
            {
                info_list.Add(room.Value.RoomInfo);
                //RoomInfo value = new RoomInfo();
                //value.RoomCapacity = room.Value.RoomInfo.RoomCapacity;
                //value.UserCount = room.Value.RoomInfo.UserCount;
                //ret.Add(room.Key, value);
            }
            return info_list;
        }

        /// <summary>
        /// 处理客户端加入房间的请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="pack"></param>
        /// <returns></returns>
        public void ActionJoinRoom(Client client, GamePack pack)
        {
            Console.WriteLine("ActionJoinRoom");
            if (client.IsLogin && client.Socket != null && client.Socket.Connected)
            {               
                Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);
                if (room == null || room.RoomRemain == 0 || room.IsUsing)
                {
                    Console.WriteLine("ActionJoinRoom:  JoinFail  room != null " + (room != null) +
                        " room.RoomRemain != 0 " + (room.RoomRemain != 0) +
                        " room.IsUsing ==  " + (room.IsUsing));

                    pack.ReturnCode = ReturnCode.ReturnFail;
                    pack.MessageType = MessageType.Room;
                    pack.ActionCode = ActionCode.ActionJoinRoom;
                }
                else
                {
                    Console.WriteLine("ActionJoinRoom:  room.JoinRoom");
                    room.JoinRoom(client);
                }               
            }
        }

        /// <summary>
        /// 处理客户端离开房间的请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="pack"></param>
        /// <returns></returns>
        public void ActionLeaveRoom(Client client, GamePack pack)
        {
            
            if (client.Socket != null && client.Socket.Connected)
            {
                Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);
                if(room == null)
                {
                    pack.ReturnCode = ReturnCode.ReturnFail;
                    pack.MessageType = MessageType.Room;
                    pack.ActionCode = ActionCode.ActionLeaveRoom;
                }
                else
                {
                    room.LeaveRoom(client);
                }
            }

        }

        /// <summary>
        /// 处理玩家准备就绪请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="pack"></param>
        public void ActionPrepared(Client client, GamePack pack)
        {
            //Console.WriteLine(pack.UserInfo.CharacterChossedID);
            //Console.WriteLine(pack.RoomInfo.RoomName);
            //Console.WriteLine(pack.UserInfo.UserName);
            //Console.WriteLine(pack.UserInfo.CharacterChossedID);
            if (pack == null || pack.RoomInfo == null || client.userInfo.CharacterChossedID == -1)
            {
                Console.WriteLine("ActionPrepared: pack != null " + (pack!=null) + 
                    " roomInfo != null " + (pack.RoomInfo != null) + 
                    " characterID = " + client.userInfo.CharacterChossedID);
                return;
            }
            
            if(client.userInfo.IsPrepared == true)
            {
                //若玩家之前就准备就绪，则返回
                Console.WriteLine("ActionPrepared:  player already prepared");
                return;
            }

            Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);

            //成功获得room后
            if(room != null)
            {
                //Console.WriteLine("ActionPrepared : get room");
                //client.userInfo.IsPrepared = true;
                room.DealPreparedMessage(client.userInfo.AccountID);
            }
        }

        /// <summary>
        /// 处理玩家取消准备请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="pack"></param>
        public void ActionPreparedCancel(Client client, GamePack pack)
        {
            Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);
            //成功取得房间后
            if(room != null)
            {
                room.DealPrepareCanceledMessage(client.userInfo.AccountID);
            }
        }

        /// <summary>
        /// 处理玩家选择角色的请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="pack"></param>
        public void ActionChooseCharacter(Client client, GamePack pack)
        {
            if (pack == null || pack.RoomInfo == null || pack.UserInfo == null)
                return;

            Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);
            if(room != null)
            {
                client.userInfo.CharacterChossedID = pack.UserInfo.CharacterChossedID;
                pack.UserInfo = client.userInfo;
                Console.WriteLine("ActionChooseCharacter broadcast:  ");
                room.BroadcastMessageForAllClient(pack);
            }
        }
    }
}
